package com.nulldev.util.web.HttpServer3.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.channels.SocketChannel;

import com.nulldev.util.io.IOUtils;
import com.nulldev.util.networking.ip.IP;

public class DummySocket extends Socket {

	private final InputStream is;
	private final OutputStream os;

	public DummySocket(final InputStream is, final OutputStream os) {
		this.is = is;
		this.os = os;
	}

	@Override
	public SocketChannel getChannel() {
		return null;
	}

	@Override
	public InetAddress getInetAddress() {
		try {
			return IP.localhost();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void bind(final SocketAddress bindpoint) throws IOException {
	}

	@Override
	public void connect(final SocketAddress endpoint) throws IOException {
	}

	@Override
	public void connect(final SocketAddress endpoint, final int timeout) throws IOException {
	}

	@Override
	public InputStream getInputStream() throws IOException {
		return this.is;
	}

	@Override
	public boolean getKeepAlive() throws SocketException {
		return false;
	}

	@Override
	public void setKeepAlive(final boolean on) throws SocketException {
	}

	@Override
	public InetAddress getLocalAddress() {
		try {
			return IP.localhost();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int getLocalPort() {
		try {
			return 2011;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public SocketAddress getLocalSocketAddress() {
		try {
			return new InetSocketAddress(2011);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean getOOBInline() throws SocketException {
		return false;
	}

	@Override
	public void setOOBInline(final boolean on) throws SocketException {
	}

	@Override
	public int getPort() {
		try {
			return 2011;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public OutputStream getOutputStream() throws IOException {
		return this.os;
	}

	@Override
	public synchronized int getReceiveBufferSize() throws SocketException {
		try {
			return IOUtils.MEMORY_ALLOC_BUFFER;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public synchronized int getSendBufferSize() throws SocketException {
		try {
			return IOUtils.MEMORY_ALLOC_BUFFER;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public SocketAddress getRemoteSocketAddress() {
		try {
			return new SocketAddress() {

				private static final long serialVersionUID = 1L;
			};
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean getReuseAddress() throws SocketException {
		return false;
	}

	@Override
	public int getSoLinger() throws SocketException {
		return -1;
	}

	@Override
	public synchronized int getSoTimeout() throws SocketException {
		return 0;
	}

	@Override
	public boolean getTcpNoDelay() throws SocketException {
		return false;
	}

	@Override
	public boolean isConnected() {
		return true;
	}

	@Override
	public boolean isBound() {
		return true;
	}

	@Override
	public boolean isClosed() {
		return false;
	}

	@Override
	public boolean isInputShutdown() {
		return false;
	}

	@Override
	public boolean isOutputShutdown() {
		return false;
	}

	@Override
	public synchronized void setReceiveBufferSize(final int size) throws SocketException {
	}

	@Override
	public synchronized void setSendBufferSize(final int size) throws SocketException {
	}

	@Override
	public void setReuseAddress(final boolean on) throws SocketException {
	}

	@Override
	public synchronized void setSoTimeout(final int timeout) throws SocketException {
	}

	@Override
	public void setTcpNoDelay(final boolean on) throws SocketException {
	}

	@Override
	public void shutdownInput() throws IOException {
	}

	@Override
	public void shutdownOutput() throws IOException {
	}

	@Override
	public int getTrafficClass() throws SocketException {
		return 0;
	}

	@Override
	public void setTrafficClass(final int tc) throws SocketException {
	}

	@Override
	public synchronized void close() throws IOException {
		IOUtils.closeAllQuietly(this.is, this.os);
	}
}
